package com.atguigu.gmall.oauth.controller;

import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.oauth.service.LoginService;
import com.atguigu.gmall.oauth.util.AuthToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 * 用户登录的控制层
 */
@RestController
@RequestMapping("/user/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @GetMapping
    public Result login(String username, String password){
        //1 校验参数
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            throw new GmallException("用户名或参数不能为空",null);
        }

        //2 调用业务层方法，获取令牌
        AuthToken authToken = loginService.login(username, password);

        //3 响应数据
        return Result.ok(authToken);
    }
}
